package com.uxhao.website.controller;


import com.uxhao.commonutils.R;
import com.uxhao.website.entity.Setting;
import com.uxhao.website.service.SettingService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author uxhao
 * @since 2022-04-15
 */
@RestController
@RequestMapping("/website/setting")
public class SettingController {
    @Autowired
    SettingService settingService;

    @Cacheable(value = "settings", key = "'getSettings'")
    @ApiOperation(value = "查看网站设置")
    @GetMapping("get")
    public R getSettings(){
        Setting setting = settingService.getOne(null);
        return R.ok().data("setting", setting);
    }

    @CacheEvict(value = "settings",allEntries=true)//清空
    @ApiOperation(value = "增加或者保存网站设置")
    @PostMapping("save")
    public R addSettings(@RequestBody Setting setting){
        boolean b;
        if(setting.getId().isEmpty()){//id为空新增
            b = settingService.save(setting);
        }else{//更新
            b = settingService.updateById(setting);
        }
        return b?R.ok():R.error();
    }
}

